package org.thss.petri.gef.editparts.policies;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.ReconnectRequest;
import org.thss.petri.gef.commands.CreateArcCommand;
import org.thss.petri.gef.commands.ReconnectArcCommand;
import org.thss.petri.model.Arc;
import org.thss.petri.model.Node;

public class NodeGraphicalNodeEditPolicy extends GraphicalNodeEditPolicy {

	@Override
	protected Command getConnectionCompleteCommand(
			CreateConnectionRequest request) {
		CreateArcCommand command = (CreateArcCommand) request.getStartCommand();
		command.setTarget((Node) getHost().getModel());
		return command;
	}

	@Override
	protected Command getConnectionCreateCommand(CreateConnectionRequest request) {
		CreateArcCommand command = new CreateArcCommand();
		command.setConnection((Arc) request.getNewObject());
		Node source = (Node) getHost().getModel();
		command.setSource(source);
		command.setNet(source.getNet());
		request.setStartCommand(command);
		return command;
	}

	@Override
	protected Command getReconnectTargetCommand(ReconnectRequest request) {
		ReconnectArcCommand command = new ReconnectArcCommand();
		command.setConnection((Arc) request.getConnectionEditPart().getModel());
		command.setNewTarget((Node) getHost().getModel());
		return command;
	}

	@Override
	protected Command getReconnectSourceCommand(ReconnectRequest request) {
		ReconnectArcCommand command = new ReconnectArcCommand();
		command.setConnection((Arc) request.getConnectionEditPart().getModel());
		command.setNewSource((Node) getHost().getModel());
		return command;
	}

}
